package com.hd.hdxgddtools.mybatis.config;

import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import lombok.Data;

import java.util.List;
import java.util.stream.Collectors;

@Data
public class InjectionConfig {
    private List<FileOutConfig> fileOutConfigs;

    public com.baomidou.mybatisplus.generator.InjectionConfig toCfg(String project, String moduleName) {
        com.baomidou.mybatisplus.generator.InjectionConfig config = new com.baomidou.mybatisplus.generator.InjectionConfig() {
            @Override
            public void initMap() {
                List<com.baomidou.mybatisplus.generator.config.FileOutConfig> collect = fileOutConfigs.stream().map(fileOutConfig ->
                        new com.baomidou.mybatisplus.generator.config.FileOutConfig(fileOutConfig.getTemplatePath()) {
                            @Override
                            public String outputFile(TableInfo tableInfo) {
                                String path = project + "/" + fileOutConfig.getOutPutLocation() + "/";
                                String fileNameSuffix = fileOutConfig.getFileNameSuffix();
                                String entityName = tableInfo.getEntityName();
                                if (fileNameSuffix.equalsIgnoreCase("service.java")) {
                                    entityName = "I" + entityName;
                                }
                                if (moduleName == null || moduleName.trim().isEmpty()) {
                                    return path + entityName + fileNameSuffix;
                                }
                                return path + moduleName + "/" + entityName + fileNameSuffix;
                            }
                        }
                ).collect(Collectors.toList());
                this.setFileOutConfigList(collect);
            }
        };
        return config;
    }
}
